package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.*;
import com.ys.entity.storage.TccLogisticsChannelMaint;
import com.ys.entity.storage.TccLogisticsChannelMaintItem;
import com.ys.service.storage.TccLogisticsChannelMaintService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/web/storage/tccLogisticsChannelMaint")
public class WebTccLogisticsChannelMaintController {
	private static final Logger logger = LoggerFactory.getLogger(WebTccLogisticsChannelMaintController.class);

	@Autowired
	private CommonApiService<TccLogisticsChannelMaint> commonApiService;
	@Autowired
	private TccLogisticsChannelMaintService tccLogisticsChannelMaintService;

	private final Long MENU_ID = MenuId.TCC_LogisticsChannelMaint;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {

		try {
			Map<String, Object> tccWarehouse = new TccWarehouse()
					.addField("A.Vck vck")
					.where("A.cck='CK00001'")
					.queryFirstMap();
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID, new ArrayList<Map<String, Object>>());
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("status", 1);
			extraParam.put("cck", "CK00001");
			extraParam.put("vck", tccWarehouse.get("vck"));
			commonApiService.toAdd(model, new TccLogisticsChannelMaint(), MenuId.TCC_LogisticsChannelMaint, extraParam, null);
			model.addAttribute("onProcess", 0);
			return "storage/tccLogisticsChannelMaint/tccLogisticsChannelMaintAdd";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	/*
	 * 详情页
	 */
	@RequiresPermissions("tccLogisticsChannelMaint:view")
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
		TccLogisticsChannelMaint entity = new TccLogisticsChannelMaint().setId(id).queryFirst();
		try {
			// 子列表配置信息
			Map<String, Object> tccLogisticsChannelMaint = new TccLogisticsChannelMaint()
					.addLeftJoin("TCC_Warehouse B ON B.Cck=A.Cck")
					.addLeftJoin("TCC_LogisticsServiceProvider E ON E.ProviderBid=A.ProviderCode")
					.addLeftJoin("TB_Country C ON C.Code=A.Country")
					.addLeftJoin("TCC_ChannelType F ON F.Id=A.ChannelValueId")
					.addLeftJoin("TCC_ChannelType G ON G.Id=A.ChannelBaoguanId")
					.addLeftJoin("TCC_ChannelType H ON H.Id=A.ChannelQingguanId")
					.addLeftJoin("TCC_ChannelType I ON I.Id=A.ChannelStockId")
					.addLeftJoin("TCC_ChannelType J ON J.Id=A.ChannelWorgnoId")
					.addLeftJoin("TCC_LogisticsChannelMaintItemSon D ON D.MethodId=A.Id")
					.addField("B.Vck vck,C.Name countryName,D.AdditionalMaterialBilling additionalMaterialBilling,E.Abbreviation providerName,F.ValueCode valueCode,F.Valuenm valuenm,F.Id channelvalueId")
					.addField("G.Valuenm baoguanCode,G.ValueCode baoguan,G.Id channelbaoguanId,H.Valuenm qingguanCode,H.ValueCode qingguan,H.Id channelqingguanId," +
							"I.Valuenm stockCode,I.ValueCode stock,I.Id channelstockId,J.Valuenm worgnoCode,J.ValueCode worgno,J.Id channelworgnoId")
					.setId(id)
					.queryFirstMap();
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("cck", entity.getCck());
			extraParam.put("vck", tccLogisticsChannelMaint.get("vck"));
			extraParam.put("countryName", tccLogisticsChannelMaint.get("countryName"));
			extraParam.put("providerName", tccLogisticsChannelMaint.get("providerName"));
			extraParam.put("valueCode", tccLogisticsChannelMaint.get("valueCode"));
			extraParam.put("baoguanCode", tccLogisticsChannelMaint.get("baoguanCode"));
			extraParam.put("qingguanCode", tccLogisticsChannelMaint.get("qingguanCode"));
			extraParam.put("stockCode", tccLogisticsChannelMaint.get("stockCode"));
			extraParam.put("worgnoCode", tccLogisticsChannelMaint.get("worgnoCode"));
			extraParam.put("valuenm", tccLogisticsChannelMaint.get("valuenm"));
			extraParam.put("baoguan", tccLogisticsChannelMaint.get("baoguan"));
			extraParam.put("qingguan", tccLogisticsChannelMaint.get("qingguan"));
			extraParam.put("stock", tccLogisticsChannelMaint.get("stock"));
			extraParam.put("worgno", tccLogisticsChannelMaint.get("worgno"));
			extraParam.put("channelvalueId", tccLogisticsChannelMaint.get("channelvalueId"));
			extraParam.put("channelbaoguanId", tccLogisticsChannelMaint.get("channelbaoguanId"));
			extraParam.put("channelqingguanId", tccLogisticsChannelMaint.get("channelqingguanId"));
			extraParam.put("channelstockId", tccLogisticsChannelMaint.get("channelstockId"));
			extraParam.put("channelworgnoId", tccLogisticsChannelMaint.get("channelworgnoId"));
			commonApiService.toDetail(model, id, new TccLogisticsChannelMaint(), MenuId.TCC_LogisticsChannelMaint, extraParam, null);
			List<TccLogisticsChannelMaintItem> tccLogisticsOne = getLogisticsOne(id);
			List<TccLogisticsChannelMaintItemSon> tccLogisticsTwo = getLogisticsTwo(id);
			List<TccAdditionalBillingMaint> tccLogisticsFour = getLogisticsFour(id);
			List<TccAdditionalBillingMaintTwo> tccLogisticsFive = getLogisticsFive(id);

			Long LogisticsChannelMaintId = entity.getId();

			TccLogisticsChannelMaintDetailDetail detailList = new TccLogisticsChannelMaintDetailDetail(id, tccLogisticsOne, tccLogisticsTwo, tccLogisticsFour, tccLogisticsFive);
			model.addAttribute("detail", detailList);
			model.addAttribute("LogisticsChannelMaintId", LogisticsChannelMaintId);
			model.addAttribute("onProcess", 0);
			return "storage/tccLogisticsChannelMaint/tccLogisticsChannelMaintDetail";
		} catch (Exception e) {
			logger.error("物流渠道维护详情页面出错：", e.getMessage(), e);
			return W.error(model, e.getMessage());
		}
	}
	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
		TccLogisticsChannelMaint entity = new TccLogisticsChannelMaint().setId(id).queryFirst();
		try {
			// 子列表配置信息
			Map<String, Object> tccLogisticsChannelMaint = new TccLogisticsChannelMaint()
					.addLeftJoin("TCC_Warehouse B ON B.Cck=A.Cck")
					.addLeftJoin("TCC_LogisticsServiceProvider E ON E.ProviderBid=A.ProviderCode")
					.addLeftJoin("TB_Country C ON C.Code=A.Country")
					.addLeftJoin("TCC_ChannelType F ON F.Id=A.ChannelValueId")
					.addLeftJoin("TCC_ChannelType G ON G.Id=A.ChannelBaoguanId")
					.addLeftJoin("TCC_ChannelType H ON H.Id=A.ChannelQingguanId")
					.addLeftJoin("TCC_ChannelType I ON I.Id=A.ChannelStockId")
					.addLeftJoin("TCC_ChannelType J ON J.Id=A.ChannelWorgnoId")
					.addLeftJoin("TCC_LogisticsChannelMaintItemSon D ON D.MethodId=A.Id")
					.addField("B.Vck vck,C.Name countryName,D.AdditionalMaterialBilling additionalMaterialBilling,E.Abbreviation providerName,F.ValueCode valueCode,F.Valuenm valuenm,F.Id channelvalueId")
					.addField("G.Valuenm baoguanCode,G.ValueCode baoguan,G.Id channelbaoguanId,H.Valuenm qingguanCode,H.ValueCode qingguan,H.Id channelqingguanId," +
							"I.Valuenm stockCode,I.ValueCode stock,I.Id channelstockId,J.Valuenm worgnoCode,J.ValueCode worgno,J.Id channelworgnoId")
					.setId(id)
					.queryFirstMap();
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("cck", entity.getCck());
			extraParam.put("vck", tccLogisticsChannelMaint.get("vck"));
			extraParam.put("countryName", tccLogisticsChannelMaint.get("countryName"));
			extraParam.put("providerName", tccLogisticsChannelMaint.get("providerName"));
			extraParam.put("valueCode", tccLogisticsChannelMaint.get("valueCode"));
			extraParam.put("baoguanCode", tccLogisticsChannelMaint.get("baoguanCode"));
			extraParam.put("qingguanCode", tccLogisticsChannelMaint.get("qingguanCode"));
			extraParam.put("stockCode", tccLogisticsChannelMaint.get("stockCode"));
			extraParam.put("worgnoCode", tccLogisticsChannelMaint.get("worgnoCode"));
			extraParam.put("valuenm", tccLogisticsChannelMaint.get("valuenm"));
			extraParam.put("baoguan", tccLogisticsChannelMaint.get("baoguan"));
			extraParam.put("qingguan", tccLogisticsChannelMaint.get("qingguan"));
			extraParam.put("stock", tccLogisticsChannelMaint.get("stock"));
			extraParam.put("worgno", tccLogisticsChannelMaint.get("worgno"));
			extraParam.put("channelvalueId", tccLogisticsChannelMaint.get("channelvalueId"));
			extraParam.put("channelbaoguanId", tccLogisticsChannelMaint.get("channelbaoguanId"));
			extraParam.put("channelqingguanId", tccLogisticsChannelMaint.get("channelqingguanId"));
			extraParam.put("channelstockId", tccLogisticsChannelMaint.get("channelstockId"));
			extraParam.put("channelworgnoId", tccLogisticsChannelMaint.get("channelworgnoId"));
			commonApiService.toEdit(model, id, new TccLogisticsChannelMaint(), MenuId.TCC_LogisticsChannelMaint, extraParam, null);
			List<TccLogisticsChannelMaintItem> tccLogisticsOne = getLogisticsOne(id);
			List<TccLogisticsChannelMaintItemSon> tccLogisticsTwo = getLogisticsTwo(id);
			List<TccAdditionalBillingMaint> tccLogisticsFour = getLogisticsFour(id);
			List<TccAdditionalBillingMaintTwo> tccLogisticsFive = getLogisticsFive(id);


			Long LogisticsChannelMaintId = entity.getId();

			TccLogisticsChannelMaintDetailEdit detailList = new TccLogisticsChannelMaintDetailEdit(id, tccLogisticsOne, tccLogisticsTwo, tccLogisticsFour,tccLogisticsFive);
			model.addAttribute("detail", detailList);
			model.addAttribute("LogisticsChannelMaintId", LogisticsChannelMaintId);
			model.addAttribute("onProcess", 0);
			return "storage/tccLogisticsChannelMaint/tccLogisticsChannelMaintEdit";
		} catch (Exception e) {
			logger.error("物流渠道维护编辑页面出错：", e.getMessage(), e);
			return W.error(model, e.getMessage());
		}
	}

	//获取计重梯度计费标准数据
	public List<TccLogisticsChannelMaintItem> getLogisticsOne(Long id) {
		List<TccLogisticsChannelMaintItem> tccLogisticsOne = new TccLogisticsChannelMaintItem()
				.addLeftJoin("TCC_LogisticsChannelMaint C ON A.MethodId = C.Id")
				.setMethodId(id)
				.setChannelMaintType(0)
				.queryList();
		return tccLogisticsOne;
	}


	//获取材质附加计费标准数据
	public List<TccLogisticsChannelMaintItemSon> getLogisticsTwo(Long id) {
		List<TccLogisticsChannelMaintItemSon> tccLogisticsTwo = new TccLogisticsChannelMaintItemSon()
				.setMethodId(id)
				.queryList();
		return tccLogisticsTwo;
	}

	//获取目的地附加计费标准数据
	public List<TccAdditionalBillingMaint> getLogisticsFour(Long id) {
		List<TccAdditionalBillingMaint> tccLogisticsFour = new TccAdditionalBillingMaint()
				.setMethodId(id)
				.queryList();
		return tccLogisticsFour;
	}

	//获取可发送目的
	public List<TccAdditionalBillingMaintTwo> getLogisticsFive(Long id) {
		List<TccAdditionalBillingMaintTwo> tccLogisticsFive = new TccAdditionalBillingMaintTwo()
				.setMethodId(id)
				.queryList();
		return tccLogisticsFive;
	}


	@Data
	@AllArgsConstructor
	private static class TccLogisticsChannelMaintDetailEdit {
		private Long id;
		private List<TccLogisticsChannelMaintItem> tccLogisticsOne; // 计重梯度计费标准
		private List<TccLogisticsChannelMaintItemSon> tccLogisticsTwo; // 材质附加计费标准
		private List<TccAdditionalBillingMaint> tccLogisticsFour; // 目的地附加计费标准
		private List<TccAdditionalBillingMaintTwo> tccLogisticsFive; // 可发送的目的


	}

	@Data
	@AllArgsConstructor
	private static class TccLogisticsChannelMaintDetailDetail {
		private Long id;
		private List<TccLogisticsChannelMaintItem> tccLogisticsOne; // 计重梯度计费标准
		private List<TccLogisticsChannelMaintItemSon> tccLogisticsTwo; // 材质附加计费标准
		private List<TccAdditionalBillingMaint> tccLogisticsFour; // 目的地附加计费标准
		private List<TccAdditionalBillingMaintTwo> tccLogisticsFive; // 可发送的目的


	}
	/*
	 * 选择物流渠道（物流计划单使用）
	 */
	@GetMapping("/toEditPlan")
	public String toEditPlan(Model model, HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> params) {
		try {
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("cck", params.get("cck"));
			extraParam.put("list", params.get("list"));
			extraParam.put("country", params.get("country"));
			extraParam.put("transportationId", params.get("transportationId"));
			extraParam.put("nameLogsticsClearance", params.get("nameLogsticsClearance"));
			commonApiService.toAdd(model, new TccLogisticsChannelMaint(), MenuId.TCC_LogisticsChannelMaintPlan, extraParam, null);
			return "storage/tccLogisticsChannelMaint/toEditPlan";
		} catch (Exception e) {
			logger.error("选择物流渠道页面出错：", e.getMessage(), e);
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 选择物流渠道拼票（物流计划单使用）
	 */
	@GetMapping("/toEditPlanTicket")
	public String toEditPlanTicket(Model model, HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> params) {
		try {
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("cck", params.get("cck"));
			extraParam.put("list", params.get("list"));
			extraParam.put("country", params.get("country"));
			extraParam.put("transportationId", params.get("transportationId"));
			extraParam.put("nameLogsticsClearance", params.get("nameLogsticsClearance"));
			extraParam.put("fbaWarehouseAddr", params.get("fbaWarehouseAddr"));
			extraParam.put("tableId", params.get("tableId"));
			extraParam.put("rowClickId", params.get("rowClickId"));

			//子列表配置
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCC_FbaLogisticsPlanPs, new ArrayList<Map<String, Object>>());

			commonApiService.toAdd(model, new TccLogisticsChannelMaint(), MenuId.TCC_FbaLogisticsPlanPs, extraParam, subGridConfig);
			return "storage/tccLogisticsChannelMaint/toEditPs";
		} catch (Exception e) {
			logger.error("选择物流渠道页面出错：", e.getMessage(), e);
			return W.error(model, e.getMessage());
		}


	}

}